/**
 * Created by yangsl on 2016/6/23.
 */
var Common = function () {
    var postAjaxRequest = function($url,$data,$callback) {
        var csrfParam = $("meta[name=csrf-param]").attr("content");
        var csrfToken =  $("meta[name=csrf-token]").attr("content");
        var obj = {};
        obj[csrfParam] = csrfToken;
        $data = $.extend(obj,$data);
        $.ajax({
            type: 'post',
            url: $url ,
            dataType: 'json',
            data: $data,
            success: function(result){
                if(result.status == 'success') {
                    if(result.msg != undefined) {
                        toastr['success'](result.msg, '');
                    }
                    if($.isFunction($callback)){
                        $callback(result);
                    }
                } else {
                    toastr[result.status](result.msg, '');
                }
            } ,
        });
    };
    return {
        postAjaxRequest: postAjaxRequest
    }
}();

jQuery(document).ready(function() {
    //删除
    $('.btn-delete').click(function(e){
        e.preventDefault();
        var self = $(this);
        var url = $(this).attr("href");

        bootbox.confirm("确定"+ $.trim(self.text())+"?", function(res) {
            if(res){
                Common.postAjaxRequest(url,{},function () {
                    self.parents('tr').remove();
                });
            }
        });
    });
    //详情
    $('.btn-detail').click(function(e){
    	e.preventDefault();
    	var self = $(this);
        var url = "/index.php?r=tobuy%2Fdetail";
        var id = self.attr('data-id');
        var body = $("#view-detail modal-body");
        $.getJSON(url, {'id':id}, function(callback){
        	if(callback.status=='success'){
        		console.log(callback.msg);
        		$.each(callback.msg, function(i,n){
        			$("#m-username").html(n.Username);
        			$("#m-mobile").html(n.Mobile);
        			$("#m-address").html(n.Address);
        			$("#m-title").html(n.Title);
        			var data = '';
        			$.each(n.info, function(j,m){
        				data += "<div style='width:20%;height:36px;line-height:36px;float:left;text-align:center;margin-top:10px;'>"+m.CateName+"</div>"+
        									"<div style='width:20%;height:36px;line-height:36px;float:left;text-align:center;margin-top:10px;'>"+m.SpecName+"</div>"+
        									"<div style='width:20%;height:36px;line-height:36px;float:left;text-align:center;margin-top:10px;'>"+m.Inventory+"</div>"+
        									"<div style='width:20%;height:36px;line-height:36px;float:left;text-align:center;margin-top:10px;'>"+m.Info+"</div>"+
        									"<div style='width:20%;height:36px;line-height:36px;float:left;text-align:center;margin-top:10px;'>"+n.TimeEnd+"</div>"+
        									"<div style='clear:both;'></div>";
        			})
        			$("#m-list").html(data);
        		})
        	}
        })
    })
    
    $(".checker").on('click',function(){
        var self = $(this);
        self.find("span").toggleClass("checked");
        if (self.find("span").hasClass("checked")) {
            self.find("input[type='checkbox']").prop("checked", true);
        } else {
            self.find("input[type='checkbox']").prop("checked", false);
        }
    });

    $(".group-checkable").on('click', function() {
        var ifCheck = $(this).find("span").hasClass("checked");
        if(ifCheck) {
            $(this).parents("thead").siblings('tbody').find(".checker span").addClass("checked");
        } else {
            $(this).find("span").removeClass("checked");
            $(this).parents("thead").siblings('tbody').find(".checker span").removeClass("checked");
        }
        var checkboxs = $(this).parents("thead").siblings('tbody').find(".checker");
        $.each(checkboxs, function(index, checkbox){
            if($(checkbox).find("input[type='checkbox']").prop("checked") != ifCheck) {
                $(checkbox).find("input[type='checkbox']").prop("checked", ifCheck);
            }
        });
    });

});

